package com.lb.jwitter.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;

import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;

import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.service.TblCTDTDMTietHocService;
import com.lb.jwitter.service.TblDKMHDotDK;
import com.lb.jwitter.service.TblDKMHKetquaService;
import com.lb.jwitter.service.TblDKMHTaiKhoanHocVien;

import com.lb.jwitter.service.TblThongTinTKBChiTiet;
import com.lb.jwitter.util.ApplicationContextProvider;
import com.lb.jwitter.util.KetQuaDangKy;
import com.lb.jwitter.util.KiemTraDangKy;
import com.lb.jwitter.util.Message;
import com.lb.jwitter.util.QuyenDangKy;
import com.lb.jwitter.util.ThoiKhoaBieuDuKien;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

@ContextConfiguration(locations="classpath:beans.xml") 
public class BoXoaKetQuaDangKyAction {

	private TblDKMHTaiKhoanHocVien tblDKMHTaiKhoanHocVien;
	public TblDKMHTaiKhoanHocVien getTblDKMHTaiKhoanHocVien() {
		return tblDKMHTaiKhoanHocVien;
	}

	public void setTblDKMHTaiKhoanHocVien(
			TblDKMHTaiKhoanHocVien tblDKMHTaiKhoanHocVien) {
		this.tblDKMHTaiKhoanHocVien = tblDKMHTaiKhoanHocVien;
	}

	private int soTietTrongNgay;

	public int getSoTietTrongNgay() {
		return soTietTrongNgay;
	}

	
	private List<KetQuaDangKy> ketQuaDangKys;
	public List<KetQuaDangKy> getKetQuaDangKys() {
		return ketQuaDangKys;
	}

	public void setKetQuaDangKys(List<KetQuaDangKy> ketQuaDangKys) {
		this.ketQuaDangKys = ketQuaDangKys;
	}
	
	private int ketquaId;
	public int getKetquaId() {
		return ketquaId;
	}

	public void setKetquaId(int ketquaId) {
		this.ketquaId = ketquaId;
	}

	private TblCTDTDMTietHocService tblCTDTDMTietHocService;

	private String msg;
	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	private String code;
	
	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	@Autowired // Injected by Spring 
	ApplicationContextProvider contextProvider; 
	 	 
	public String execute() throws Exception
	{

		Map session = ActionContext.getContext().getSession();
		tblDKMHTaiKhoanHocVien =  (TblDKMHTaiKhoanHocVien) session.get("taiKhoanHocVien");
	     if( tblDKMHTaiKhoanHocVien == null) return "noprivilege";
	     
	     Integer dotDKId = (Integer) session.get("dotDKId");
	     
	    ApplicationContext context = contextProvider.getApplicationContext(); 

	    tblCTDTDMTietHocService = (TblCTDTDMTietHocService) context.getBean("tbl_CTDT_DM_TietHocClient");
	    TblDKMHKetquaService tblDKMHKetquaService = (TblDKMHKetquaService) context.getBean("tbl_DKMH_KetquaClient");
	    

	    
    	ketQuaDangKys = (List<KetQuaDangKy>) session.get("ketQuaDangKys");
    	boolean trongHanDK = (Boolean) session.get("trongHanDK");
	    QuyenDangKy quyenDK = (QuyenDangKy) session.get("quyenDK");
	    
	    if(trongHanDK && quyenDK.getUpdate()){
	    	
	    	for(KetQuaDangKy ketqua : ketQuaDangKys){
	    		
	    		if(ketqua.getKetqua().getId().equals(new Long(ketquaId))){
	
	    			//kiem tra trung TKB
	    			{
	    				Message message = new Message();
		    			boolean trungTKB = KiemTraDangKy.kiemTraTrungTKB(ketqua, ketQuaDangKys, message);
		    			if(trungTKB){
		    				code = "ERROR";
		    				msg = message.getMsg();
		    				
		    				return Action.ERROR;
		    			}
	    			}

	    			//khong bi dung do => cap nhat CSDL
	    			ketqua.setDelete(false);
	    			
	    			 DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 
	    			    Date dd = new Date(); 
	    			    GregorianCalendar c = new GregorianCalendar(); 
	    			    c.setTime(dd);
	    			    XMLGregorianCalendar d = DatatypeFactory.newInstance().newXMLGregorianCalendar(c); 
	    				ketqua.getKetqua().setNgayCapNhat(d);
	    	
	    				
	    			if(ketqua.getKetqua().getMaDieuChinh().equals("BH"))
	    				ketqua.getKetqua().setMaDieuChinh("BS");
	    			else if(ketqua.getKetqua().getMaDieuChinh().equals("HH"))
	    				ketqua.getKetqua().setMaDieuChinh("HC");
	    			
	    			ketqua.getKetqua().setMaDieuChinh2(ketqua.getKetqua().getNLMHId().getId().toString());
	    			ketqua.getKetqua().setXacNhanYN(false);
	    			//cap nhat database
	    			tblDKMHKetquaService.updatetblDKMHKetqua(ketqua.getKetqua());
	    			
	    			break;
	    		}
	    	}
 	    }else{
	    	code = "ERROR";
	    	msg = "Đã hết hạn hiệu chỉnh!!!";
	    	
	    	return Action.ERROR;
	    }
    	

	    List<ConditionReport> cons = new ArrayList<ConditionReport>();
    	{ 
	        ConditionReport con = new ConditionReport(); 
	        con.setCol("ma");
	        con.setValue1("0");
	        con.setOperator("ne");
	        cons.add(con);	
	   }
	    soTietTrongNgay = tblCTDTDMTietHocService.counttblCTDTDMTietHocByCondition(cons);
	    
	    session.put("ketQuaDangKys", ketQuaDangKys);
		return Action.SUCCESS;

	}


	
	
	
	
	
}
